﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Games.WPF.UI
{
    public class ResizeThumb : Thumb
    {
        #region Constructors

        public ResizeThumb()
        {
            DragDelta += new DragDeltaEventHandler(ResizeThumb_DragDelta);
        }

        #endregion

        #region Event Handlers

        private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if ((designerItem != null) && (designer != null && designerItem.IsSelected))
            {
                double minLeft = double.MaxValue;
                double minTop = double.MaxValue;
                double minDeltaHorizontal = double.MaxValue;
                double minDeltaVertical = double.MaxValue;

                // Drag limits are set by these parameters: canvas top, canvas left, minHeight, minWidth.
                // Calculate min value for each parameter for each item:
                foreach (DesignerItem item in designer.SelectedItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop);

                    minDeltaVertical = Math.Min(minDeltaVertical, item.ActualHeight - item.MinHeight);
                    minDeltaHorizontal = Math.Min(minDeltaHorizontal, item.ActualWidth - item.MinWidth);
                }

                double dragDeltaVertical, dragDeltaHorizontal;
                foreach (DesignerItem item in designer.SelectedItems)
                {
                    if (item != null)
                    {
                        switch (base.VerticalAlignment)
                        {
                            case System.Windows.VerticalAlignment.Bottom:
                                dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                                item.Height = item.ActualHeight - dragDeltaVertical;
                                break;
                            case System.Windows.VerticalAlignment.Top:
                                double top = Canvas.GetTop(item);
                                dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                                Canvas.SetTop(item, top + dragDeltaVertical);
                                item.Height = item.ActualHeight - dragDeltaVertical;
                                break;
                            default:
                                break;
                        }

                        switch (base.HorizontalAlignment)
                        {
                            case System.Windows.HorizontalAlignment.Left:
                                double left = Canvas.GetLeft(item);
                                dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                                Canvas.SetLeft(item, left + dragDeltaHorizontal);
                                item.Width = item.ActualWidth - dragDeltaHorizontal;
                                break;
                            case System.Windows.HorizontalAlignment.Right:
                                dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                                item.Width = item.ActualWidth - dragDeltaHorizontal;
                                break;
                            default:
                                break;
                        }
                    }
                }
                e.Handled = true;
            }
        }

        #endregion
    }
}